package com.peycard.persistence.dao;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Set;

import javax.ejb.Stateless;
import javax.persistence.Query;

import com.peycard.persistence.PersistenceException;
import com.peycard.persistence.dao.local.CompanyDAOLocal;
import com.peycard.persistence.entities.Company;
import com.peycard.persistence.entities.Email;

/**
 * Session Bean implementation class CompanyDAO
 */
@Stateless
public class CompanyDAO extends GenericDAOImpl<Company, Long> implements CompanyDAOLocal {

    public CompanyDAO() {
    }

    @Override
    public Company save(String commercialName, String socialReason, String giro, String rfc, String infonavit, String legalRepresent, String stps,
    		String stpsRepresent, String country, String state, String address, String extNumb, String subur, String zipCode, String tel1, String tel2,
    		String fax, String email, String webPage, String activeCompany, String userCommercial) throws PersistenceException{
    	try{
    		Company c = new Company();
        	c.setCountry(country);
        	BigDecimal faxbd = new BigDecimal(fax);
        	c.setFax(faxbd);
        	c.setInfonavit(infonavit);
        	c.setTradename(commercialName);
        	BigDecimal activebd = new BigDecimal(activeCompany);
        	c.setIsActive(activebd);
        	c.setLegalRepresantive(legalRepresent);
        	BigDecimal numberInEx = new BigDecimal(extNumb);
        	c.setNumberInEx(numberInEx);
        	BigDecimal postalCode = new BigDecimal(zipCode);
        	c.setPostalCode(postalCode);
        	c.setRfc(rfc);
        	Timestamp saveDate = new Timestamp(System.currentTimeMillis());
        	c.setSaveData(saveDate);
        	c.setSocialReason(socialReason);
        	c.setState(state);
        	c.setStps(stps);
        	c.setStpsRepresentative(stpsRepresent);
        	c.setStreet(address);
        	c.setSuburb(subur);
        	BigDecimal tele1 = new BigDecimal(tel1);
        	c.setTelephone1(tele1);
        	BigDecimal tele2 = new BigDecimal(tel2);
        	c.setTelephone2(tele2);
        	c.setUsername(userCommercial);
        	c.setWeb(webPage);
        	Company company = super.create(c);
        	Email emailCompany = new Email();
        	emailCompany.setCompany(c);
        	emailCompany.setEmail(email);
        	emailCompany.setUsername(userCommercial);
        	emailCompany.setIsActive(BigDecimal.valueOf(1));
        	emailCompany.setSaveData(saveDate);
        	getEntityManager().persist(emailCompany);
        	return company;
    	}catch(Exception e){
    		throw new PersistenceException(e);
    	}
    }
    
    @Override
    public Company update(String commercialName, String socialReason, String giro, String rfc, String infonavit, String legalRepresent, String stps,
    		String stpsRepresent, String country, String state, String address, String extNumb, String subur, String zipCode, String tel1, String tel2,
    		String fax, String email, String webPage, String activeCompany, String userCommercial, String idCompany) throws PersistenceException{
    	try{    		
    		Company c = super.read(Long.parseLong(idCompany));
    		c.setCountry(country);
    		c.setTradename(commercialName);
        	BigDecimal faxbd = new BigDecimal(fax);
        	c.setFax(faxbd);
        	c.setInfonavit(infonavit);
        	BigDecimal activebd = new BigDecimal(activeCompany);
        	c.setIsActive(activebd);
        	c.setLegalRepresantive(legalRepresent);
        	BigDecimal numberInEx = new BigDecimal(extNumb);
        	c.setNumberInEx(numberInEx);
        	BigDecimal postalCode = new BigDecimal(zipCode);
        	c.setPostalCode(postalCode);
        	Timestamp saveDate = new Timestamp(System.currentTimeMillis());
        	c.setSaveData(saveDate);
        	c.setSocialReason(socialReason);
        	c.setState(state);
        	c.setStps(stps);
        	c.setStpsRepresentative(stpsRepresent);
        	c.setStreet(address);
        	c.setSuburb(subur);
        	BigDecimal tele1 = new BigDecimal(tel1);
        	c.setTelephone1(tele1);
        	BigDecimal tele2 = new BigDecimal(tel2);
        	c.setTelephone2(tele2);
        	c.setUsername(userCommercial);
        	c.setWeb(webPage);
        	Company company = super.update(c);
        	Set<Email> mails = c.getEmails();
        	for(Email mail : mails){
        		if(!mail.getEmail().equals(email)){
        			mail.setEmail(email);
        			getEntityManager().merge(mail);
        		}
        	}
        	return company;
    	}catch(Exception e){
    		throw new PersistenceException(e);
    	}
    }
    
    @Override
    public void setDown(String idCompany, String adminUser) throws PersistenceException{
		try{
			Company c = super.read(Long.parseLong(idCompany));
			c.setUsername(adminUser);
			c.setIsActive(BigDecimal.valueOf(0));
			super.update(c);
		}catch(Exception e){
			throw new PersistenceException(e);
		}
	}
    
    @Override
    public List<Company> getAll() throws PersistenceException{
    	try{
    		Query query = getEntityManager().createNamedQuery("findAllCompanies");
    		List<Company> list = query.getResultList();
    		return list;
    	}catch(Exception e){
    		throw new PersistenceException(e);
    	}
    }
}
